package com.fudan.biz.data.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.biz.data.entity.DataWarehouse;
import com.fudan.biz.data.service.DataWarehouseService;
import com.fudan.es.indices.IndexStatus;
import com.fudan.es.service.IndexAdminService;
import com.fudan.framework.base.DaoService;
import com.fudan.framework.base.DataController;
import com.fudan.framework.entity.TableJson;

@Controller
@RequestMapping("biz/data/dw")
public class DataWarehouseController extends DataController<DataWarehouse>{

	@Autowired
	private DataWarehouseService dataWarehouseService;
	@Autowired
	private IndexAdminService indexAdminService;

	@RequestMapping("dw_list")
	@ResponseBody
	public TableJson dwList(HttpServletRequest request) {
		return TableJson.build(dataWarehouseService.pages(request), dataWarehouseService.countAll());
	}

	@RequestMapping("query_dw_list")
	@ResponseBody
	public TableJson dwQuery(HttpServletRequest request) {
		String field = request.getParameter("search_field");
		String val = request.getParameter("search_value");
		if(field!=null && val!=null &&!field.equals("")&&!val.equals("")) {
			return TableJson.build(dataWarehouseService.findAllByFuzzyQuery(field, val, 1, 10),10);
		}else {
			return TableJson.build(dataWarehouseService.pages(request),10);
		}
	}

	@RequestMapping("query_dw_indices_list")
	@ResponseBody
	public TableJson dwIndex(HttpServletRequest request) {
		String val = request.getParameter("search_value");
		String dwId = request.getParameter("dw_id");
		if(dwId == null || dwId.equals("")) {
			return TableJson.build(201, "数据仓库无法获取");
		}
		if(val==null || val.equals("")) {
			List<IndexStatus> indices = indexAdminService.indices(dwId);
			return TableJson.build(indices,indices.size());
		}else {
			List<IndexStatus> indices = indexAdminService.indices(dwId,val);
			return TableJson.build(indices,indices.size());
		}
	}

	@Override
	protected DaoService<DataWarehouse> injectService() {
		return dataWarehouseService;
	}

	@Override
	protected String pathPrefix() {
		return "biz/dw";
	}
}
